<?php

namespace app\web\controller;

use think\Db;
use app\common\model\ProductCategory as CategoryModel;

/**
 * 库存管理--商品分类控制器
 * @package app\index\controller
 */
class ProductCategory extends Auth
{
    public $noNeedLogin = '';

    /**
     * 库存管理--商品分类---添加或者修改分类
     * token
     * name
     * sort
     * description
     * product_category_id
     */
    public function edit(){

        $data = $this->request->post();

        $arr = ['name'=>$data['name'],'sort'=>$data['sort'],'description'=>$data['description']];



        if (key_exists('product_category_id',$data)&&isset($data['product_category_id']))
        {
            $result = CategoryModel::where('id',$data['product_category_id'])->update($arr);

        }else{
            $arr['store_id']=$this->userInfo['store_id'];
            $arr['pid'] = 0;
            $arr['status'] = 1;
            $result = CategoryModel::create($arr);
        }

        $result?$this->result('操作成功','',200):$this->result('修改分类信息与原信息相同','',204);
    }

    /**
     * 库存管理--商品分类--分类列表
     * token
     */
    public function lists()
    {
        if($this->request->isPost()){
            $limit = $this->request->post('limit');
            if(!isset($limit) ||empty($limit)){
                $limit = 10;
            }
            $list = CategoryModel::where('store_id',$this->userInfo['store_id'])
                ->field('id,name,description,status,sort')
                ->paginate($limit);

            $this->result('获取成功',$list,200);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 库存管理--商品分类--删除分类
     * token
     */
    public function remove()
    {
        $data = $this->request->post();

        if (empty($data['goods_category_id'])) $this->result('缺少必要参数','',204);

        $product_data = DB::name('qt_product')
            ->where([
                'store_id' => $this->userInfo['store_id'],
                'status' => 1,
                'product_category_id' => $data['goods_category_id'],
            ])->find();
        if (!empty($product_data)) {
            $this->result('分类下还有商品，不能删除分类！','',204);
        }
        $result = CategoryModel::destroy($data['goods_category_id']);

        $result?$this->result('删除成功','',200):$this->result('删除失败','',204);
    }


}
